<?php
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller {

    public function __construct(){

	     parent::__construct();

	     $nowac = CONTROLLER_NAME.'-'.ACTION_NAME;
	     // var_dump($nowac);exit;
	     $admin_id = session('manage_id');
	     $admin_name = session('manage_name');
	     $login_ac = 'manage-login,manage-verify,manage-logout';


	     if(empty($admin_id) && stripos($login_ac,$nowac)===false){
	     	$js = <<<eof
	     	<script>
	     		$modulturl = __MODULE__;
	     		window.top.location.href="{$modulturl}/Manage/login";
	     	</script>
eof;

	     	echo $js;
	     	$this->redirect('Manage/login');
	     	exit;
	     }elseif(!empty($admin_id)){



		     $admin_roles = M('mage_role')->field('role_id')->where(['mage_id' =>$admin_id])->select();
		     
			 $admin_roles = array_column($admin_roles, 'role_id');
			 $where['role_id'] = array('in',$admin_roles);

			 $admin_auths = M('role_auth')->field('auth_id')->where($where)->select();
			 $admin_auths = array_unique(array_column($admin_auths, 'auth_id'));

			 $where = array();

			 $where['auth_id'] = array('in',$admin_auths);

			 $ids = M('auth')->where($where)->field("concat(auth_controller,'-',auth_action) as ids")->select();
			 foreach($ids as $k => $v){
			 	if($v=='-'){
			 		unset($ids[$k]);
			 	}
			 }
			 $allow_ac = implode(',',array_column($ids, 'ids'));

			 
			 // var_dump($allow_ac);die;


			 if(stripos($allow_ac, $nowac) === false && stripos($login_ac,$nowac) === false){
			 	exit('没有权限访问');
			 }
		 
	     }
     // var_dump(MODULE_NAME);
     // exit;

	     if(strtolower(MODULE_NAME) == 'admin' && stripos($login_ac,$nowac)===false){
	     	$i = 0;
	     	$au = M('auth');
	     	$aus = $au->select();//查找权限列表
			$li = $this->get_list($aus);
			
			$this->assign('li',$li);

	     }



    }

    public function get_list($array,$pid=0){
		$list = array();

		foreach($array as $vo){
			if($vo['pid'] == $pid){
				$child = $this->get_list($array,$vo['auth_id']);
				$vo['child'] = $child;
				$list[] = $vo;
			}
		}

		return $list;
	}

}